using Machine.Specifications;
using Unbound.Tests.Unbinding.TestObjects;

namespace Unbound.Tests.Unbinding.Specifications.ComplexArrays
{
	[Subject("Unbinding")]
	public class arrays_of_complex_types_with_array_properties :
		unbinding<ComplexTypeWithArrayProperty[]>
	{
		Establish context =
			() =>
				{
					Prefix = "foo";
					Request = _request;
				};

		Behaves_like<behavior> a_working_unbinder;
      
		protected static readonly ComplexTypeWithArrayProperty[] _request =
			new[]
				{
					new ComplexTypeWithArrayProperty
						{
							Text = "Something",
							Bars = new[]
							       	{
							       		new ComplexTypeWithArrayProperty.Bar {Digit = 8},
							       		new ComplexTypeWithArrayProperty.Bar {Digit = 15},
							       	}
						},
					new ComplexTypeWithArrayProperty
						{
							Text = "Another",
							Bars = new[]
							       	{
							       		new ComplexTypeWithArrayProperty.Bar {Digit = 23},
							       		new ComplexTypeWithArrayProperty.Bar {Digit = 42},
							       	}
						},
				};

		[Behaviors]
		protected class behavior : behavior<ComplexTypeWithArrayProperty[]>
		{
			It should_bind_child_arrays =
				() =>
					{
						var foo = (ComplexTypeWithArrayProperty[]) Bound;
						foo[0].Bars.Length.ShouldEqual(2);
						foo[0].Bars[0].Digit.ShouldEqual(8);
						foo[0].Bars[1].Digit.ShouldEqual(15);

						foo = (ComplexTypeWithArrayProperty[]) Bound;
						foo[1].Bars.Length.ShouldEqual(2);
						foo[1].Bars[0].Digit.ShouldEqual(23);
						foo[1].Bars[1].Digit.ShouldEqual(42);
					};

			It should_bind_each_complex_type =
				() =>
					{
						var foo = (ComplexTypeWithArrayProperty[]) Bound;
						foo.Length.ShouldEqual(2);
						foo[0].Text.ShouldEqual("Something");
						foo[1].Text.ShouldEqual("Another");
					};

			It should_bind_to_array_of_complex_types =
				() => Bound.ShouldBeOfType<ComplexTypeWithArrayProperty[]>();
		}
	}
}